package sg.edu.nus.iss.vmcs.ui;

import java.awt.GridLayout;
import java.util.Observable;
import javax.swing.JLabel;
import javax.swing.JPanel;

import sg.edu.nus.iss.vmcs.controller.TransactionController;
import sg.edu.nus.iss.vmcs.entity.AbstractStoreFactory;
import sg.edu.nus.iss.vmcs.entity.Brand;
import sg.edu.nus.iss.vmcs.entity.BrandDrink;
import sg.edu.nus.iss.vmcs.entity.DrinksBrand;
import sg.edu.nus.iss.vmcs.entity.StoreItem;
import sg.edu.nus.iss.vmcs.ui.listener.DrinkSelectionListener;
import sg.edu.nus.iss.vmcs.util.Environment;

public class DrinkSelectionBox extends Observable {
	
	private TransactionController txnController;

	private JPanel drinkSelectedBoxPanel = new JPanel();
	private boolean drinkSelected;
	private CoinInputBox cbox;
	
	private BrandDrink currentlySelectedBrand;
	
	public BrandDrink getCurrentlySelectedBrand() {
		return currentlySelectedBrand;
	}

	public void setCurrentlySelectedBrand(BrandDrink currentlySelectedBrand) {
		this.currentlySelectedBrand = currentlySelectedBrand;
	}

	public DrinkSelectionBox(CoinInputBox cbox, TransactionController txnController) {
		this.txnController = txnController;
		this.cbox = cbox;
		txnController.setDrinkSelectionBox(this);
		init();
	}
	
	private void init() {
		DrinkSelectionListener dsl = new DrinkSelectionListener(this, cbox);
		
		
		StoreItem[] brandStores = AbstractStoreFactory.getInstance(Environment.getMachineModel()).getBrandDrinkInstance().getItems();
		System.out.println(brandStores.length);
		drinkSelectedBoxPanel.setLayout(new GridLayout(brandStores.length, 1));
		for(int i=0;i<brandStores.length;i++){
			
			StoreItem item = brandStores[i];
			Brand brand = (Brand) item.getContent();
			String name = brand.getName();
			double price = brand.getPrice();
			String id = brand.getId();
			ObserverButton drink1 = new ObserverButton(name);
			drink1.setActionCommand(id);
			drink1.addActionListener(dsl);
			addObserver(drink1);
			JLabel price1 = new JLabel(""+brand.getPrice());
			//JLabel stockLabel1 = new JLabel("In Stock");
			JPanel jp1 = new JPanel();
			jp1.add(drink1); jp1.add(price1);
			//jp1.add(stockLabel1);
			drinkSelectedBoxPanel.add(jp1);
		}
		/*
		ObserverButton drink1 = new ObserverButton("Coca Cola");
		ObserverButton drink2 = new ObserverButton("Sarsi");
		ObserverButton drink3 = new ObserverButton("Pepsi");
		ObserverButton drink4 = new ObserverButton("Soya bean");
		ObserverButton drink5 = new ObserverButton("Fanta");
		
		drink1.setActionCommand("1");
		drink2.setActionCommand("2");
		drink3.setActionCommand("3");
		drink4.setActionCommand("4");
		drink5.setActionCommand("5");
		
		drink1.addActionListener(dsl);
		drink2.addActionListener(dsl);
		drink3.addActionListener(dsl);
		drink4.addActionListener(dsl);
		drink5.addActionListener(dsl);
		
		addObserver(drink1);
		addObserver(drink2);
		addObserver(drink3);
		addObserver(drink4);
		addObserver(drink5);
		
		JLabel price1 = new JLabel("120.0");
		JLabel price2 = new JLabel("120.0");
		JLabel price3 = new JLabel("120.0");
		JLabel price4 = new JLabel("120.0");
		JLabel price5 = new JLabel("120.0");
		
		JLabel stockLabel1 = new JLabel("In Stock");
		JLabel stockLabel2 = new JLabel("In Stock");
		JLabel stockLabel3 = new JLabel("In Stock");
		JLabel stockLabel4 = new JLabel("In Stock");
		JLabel stockLabel5 = new JLabel("In Stock");
		
		JPanel jp1 = new JPanel();
		JPanel jp2 = new JPanel();
		JPanel jp3 = new JPanel();
		JPanel jp4 = new JPanel();
		JPanel jp5 = new JPanel();
		
		jp1.add(drink1); jp1.add(price1); jp1.add(stockLabel1);
		jp2.add(drink2); jp2.add(price2); jp2.add(stockLabel2);
		jp3.add(drink3); jp3.add(price3); jp3.add(stockLabel3);
		jp4.add(drink4); jp4.add(price4); jp4.add(stockLabel4);
		jp5.add(drink5); jp5.add(price5); jp5.add(stockLabel5);
		
		drinkSelectedBoxPanel.setLayout(new GridLayout(5, 1));
		drinkSelectedBoxPanel.add(jp1);
		drinkSelectedBoxPanel.add(jp2);
		drinkSelectedBoxPanel.add(jp3);
		drinkSelectedBoxPanel.add(jp4);
		drinkSelectedBoxPanel.add(jp5);*/
	}
	
	public boolean isDrinkSelected() {
		return drinkSelected;
	}

	public void setDrinkSelected(boolean drinkSelected) {
		this.drinkSelected = drinkSelected;
		setChanged();
	}

	public JPanel getDrinkSelectedBoxPanel() {
		return drinkSelectedBoxPanel;
	}

	public TransactionController getTxnController() {
		return txnController;
	}
	
}